//package com.lucas.vistart;
//
//import android.os.Bundle;
//
//import androidx.appcompat.app.AppCompatActivity;
//
//
//public class LoginOrRegister extends AppCompatActivity {
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.register_info2);
//        if (getSupportActionBar() != null){
//            getSupportActionBar().hide();
//        }
//
//    }
//
//}
package com.lucas.vistart;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import com.lucas.vistart.network.OkHttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginOrRegister extends AppCompatActivity {

    private Button btn1;
    private Button btn2;
    private EditText editText1;
    private EditText editText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_or_register);
        init();
    }
    private void init()
    {
        btn1=findViewById(R.id.getIdentification);
        btn2=findViewById(R.id.Login);
        editText1 =findViewById(R.id.phnumber);
        editText2 =findViewById(R.id.cashcode);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph= editText1.getText().toString();
                System.out.println(ph);
                if(ph.equals(""))
                    Toast.makeText(LoginOrRegister.this,"手机号不能为空",Toast.LENGTH_SHORT).show();
                else
                {
                    getIdentifiction(ph);
                }
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph= editText1.getText().toString();
                String id= editText2.getText().toString();
                if(ph.equals("")||id.equals(""))
                    Toast.makeText(LoginOrRegister.this,"手机号或验证码不能为空",Toast.LENGTH_SHORT).show();
                else
                {
                    Login(ph,id);
                }
            }
        });
    }

    private void getIdentifiction(String phnumber)
    {
        JSONObject jsonParam=new JSONObject();
        jsonParam.put("tel",phnumber);
        String json = jsonParam.toJSONString();
        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody requestBody=RequestBody.Companion.create(json,mediaType);
        OkHttpUtils.sendOkHttpResponse("http://192.168.31.35:8000/artapp/login/", requestBody,  new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String data = response.body().string();
                System.out.println(data);
                //服务器返回信息做对应处理
                if(data.equals("success"))
                    Toast.makeText(LoginOrRegister.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                else if(data.equals("wrong code"))
                    Toast.makeText(LoginOrRegister.this,"服务器错误",Toast.LENGTH_SHORT).show();
                else if(data.equals("invalid"))
                    Toast.makeText(LoginOrRegister.this,"手机号码不正确",Toast.LENGTH_SHORT).show();
            }
        });
        countDown();
    }
    private void countDown()
    {
        CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
            @SuppressLint("DefaultLocale")
            @Override
            public void onTick(long millisUntilFinished) {
                btn1.setEnabled(false);
                btn1.setText(String.format("已发送(%d)",millisUntilFinished/1000));
            }

            @Override
            public void onFinish() {
                btn1.setEnabled(true);
                btn1.setText("重新获取");
            }
        };
        timer.start();
    }

    private void Login(String phnumber,String Identification)
    {
        JSONObject jsonParam=new JSONObject();
        jsonParam.put("tel",phnumber);
        jsonParam.put("code",Identification);
        String json = jsonParam.toJSONString();
        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
        RequestBody requestBody=RequestBody.Companion.create(json,mediaType);
        OkHttpUtils.sendOkHttpResponse("http://192.168.31.35:8000/artapp/auth/", requestBody,  new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String data = response.body().string();
                System.out.println(data);
                //服务器返回信息做对应处理
                if(data.equals("success"))
                    Toast.makeText(LoginOrRegister.this,"登陆成功",Toast.LENGTH_SHORT).show();
                else if(data.equals("fail"))
                    Toast.makeText(LoginOrRegister.this,"验证码错误",Toast.LENGTH_SHORT).show();
                else if(data.equals("need_register"))
                {
                    Toast.makeText(LoginOrRegister.this,"新用户注册",Toast.LENGTH_SHORT).show();
                    //Intent intent=new Intent(LoginOrRegister.this,B.this);
                    //intent.putExtra("tel",phnumber);
                    //startActivity(intent);
                }
            }
        });
    }

}